--[[
	每次返回大厅，都会获取一次用户的基本数据更新一下首页
	还有获取其他人信息，现在本node的longtcpmng上查询是否存在（在线）
	如果有，直接返回，如果没有，则从数据node上查询
]]
local filename = "getroleinfo.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"

local service = nil
local CMD = {
	service = nil
}

-- socket传来的请求，可以获取自己的信息，也可以去获取其他agent中的个人信息
-- 如果都不在，则去数据节点中获取
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
	local nilroleinfo = {
		rid = 0,
		rolename = "",
		level =  0,
		logo =  "",
		sex =  0,
		diamond = 0,
		lock_diamond = 0,
		coin = 0,
        lock_coin = 0,
		fangka = 0,
		intro = "",
        phonenum = "",
        idtrue = 0,
        ip_addr = "",
        logoframe = 0,
	}
	if 	msgbody.objrid == 0 
		or msgbody.objrid == nil
		or rid == msgbody.objrid  then
		-- 获取自己的信息返回
		return {baseinfo = service:myroleinfo()}
	else
		-- 去访问数据节点，或者访问node里面其他的agent节点
		local objrid = tonumber(msgbody.objrid)
		local agent = skynet.call(".mainnode", "lua", "cmd", "get_agent_by_rid", objrid)
		if agent then
			-- agent 在线，直接call它去得到
			local roleinfo = skynet.call(agent, "lua", "cmd", "myroleinfo")
			if roleinfo then
				return {baseinfo = roleinfo}
			end
		else
			-- 去数据节点查询，但要记得添加 other_rid
			local result 
			result = cluster.call("datanode", ".router", "select_rk", objrid, "rs_info", true)
			if result[1] ~= 0 then
				nilroleinfo.rid = objrid
				nilroleinfo.rolename	= result[2][objrid].rolename
				nilroleinfo.level		= result[2][objrid].level
				nilroleinfo.logo 		= result[2][objrid].logo
				nilroleinfo.sex 		= result[2][objrid].sex
				nilroleinfo.intro 		= result[2][objrid].intro
                nilroleinfo.phonenum    = result[2][objrid].phonenum
                nilroleinfo.idtrue      = result[2][objrid].idtrue
			end

			result = cluster.call("datanode", ".router", "select_rk", objrid, "rs_money", true)
			if result[1] ~= 0 then
				nilroleinfo.diamond	= result[2][objrid].diamond
				nilroleinfo.lock_diamond	= result[2][objrid].lock_diamond
				nilroleinfo.coin	= result[2][objrid].coin
                nilroleinfo.lock_coin	= result[2][objrid].lock_coin
				nilroleinfo.fangka 	= result[2][objrid].fangka
			end
			return {baseinfo = nilroleinfo}
		end
	end
	return {baseinfo = nilroleinfo}
end

return CMD